<template>
    <div>
        <Card shadow>
            <div class="search-con search-con-top">
                <ButtonGroup>
                    <Button :disabled="hasAuthority('BaseAppEdit}')?false:true"  class="search-btn" type="primary" @click="handleModal()">
                        <span>添加</span>
                    </Button>
                </ButtonGroup>
            </div>
            <Table :columns="columns" :data="data" :loading="loading">
                <template slot="action" slot-scope="{ row }">
                    <a :disabled="hasAuthority('BaseAppEdit')?false:true"   @click="handleModal(row)">
                        编辑</a>&nbsp;
                    <Dropdown v-show="hasAuthority('BaseAppEdit')" transfer ref="dropdown" @on-click="handleClick($event,row)">
                        <a href="javascript:void(0)">
                            <span>更多</span>
                            <Icon type="ios-arrow-down"></Icon>
                        </a>
                        <DropdownMenu slot="list">
                            <DropdownItem  name="remove">删除</DropdownItem>
                        </DropdownMenu>
                    </Dropdown>&nbsp;
                </template>
            </Table>
            <Page :total="pageInfo.total" :current="pageInfo.page" :page-size="pageInfo.limit" show-elevator show-sizer
                  show-total
                  @on-change="handlePage" @on-page-size-change='handlePageSize'></Page>
        </Card>
        <Modal v-model="modalVisible"
               :title="modalTitle"
               width="40"
               @on-cancel="handleReset">
            <div>
                <Form ref="form" :model="formItem" :rules="formItemRules" :label-width="100">
                    <FormItem label="API访问key" prop="apiKey">
                        <Input v-model="formItem.apiKey"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="API访问密钥" prop="secretKey">
                        <Input v-model="formItem.secretKey"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="app名称" prop="appName">
                        <Input v-model="formItem.appName"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="app英文名称" prop="appNameEn">
                        <Input v-model="formItem.appNameEn"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="应用图标" prop="appIcon">
                        <Input v-model="formItem.appIcon"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="app类型:server-服务应用 app-手机应用 pc-PC网页应用 wap-手机网页应用" prop="appType">
                        <Input v-model="formItem.appType"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="app描述" prop="appDesc">
                        <Input v-model="formItem.appDesc"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="移动应用操作系统:ios-苹果 android-安卓" prop="appOs">
                        <Input v-model="formItem.appOs"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="官网地址" prop="website">
                        <Input v-model="formItem.website"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="开发者ID:默认为0" prop="developerId">
                        <Input v-model="formItem.developerId"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="状态:0-无效 1-有效" prop="status">
                        <Input v-model="formItem.status"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="保留数据0-否 1-是 不允许删除" prop="isPersist">
                        <Input v-model="formItem.isPersist"  placeholder="请输入内容"></Input>
                    </FormItem>
                </Form>
                <div class="drawer-footer">
                    <Button type="default" @click="handleReset">取消</Button>&nbsp;
                    <Button type="primary" @click="handleSubmit" :loading="saving">保存</Button>
                </div>
            </div>
        </Modal>
    </div>
</template>

<script>
    import BaseApp from '@/api/BaseApp'

    export default {
        name: 'BaseApp',
        data () {
            return {
                loading: false,
                saving: false,
                modalVisible: false,
                modalTitle: '',
                pageInfo: {
                    total: 0,
                    page: 1,
                    limit: 10
                },
                formItemRules: {
                    apiKey: [
                        {required: true, message: 'API访问key不能为空', trigger: 'blur'}
                    ],
                    secretKey: [
                        {required: true, message: 'API访问密钥不能为空', trigger: 'blur'}
                    ],
                    appName: [
                        {required: true, message: 'app名称不能为空', trigger: 'blur'}
                    ],
                    appNameEn: [
                        {required: true, message: 'app英文名称不能为空', trigger: 'blur'}
                    ],
                    appIcon: [
                        {required: true, message: '应用图标不能为空', trigger: 'blur'}
                    ],
                    appType: [
                        {required: true, message: 'app类型:server-服务应用 app-手机应用 pc-PC网页应用 wap-手机网页应用不能为空', trigger: 'blur'}
                    ],
                    appDesc: [
                        {required: true, message: 'app描述不能为空', trigger: 'blur'}
                    ],
                    appOs: [
                        {required: true, message: '移动应用操作系统:ios-苹果 android-安卓不能为空', trigger: 'blur'}
                    ],
                    website: [
                        {required: true, message: '官网地址不能为空', trigger: 'blur'}
                    ],
                    developerId: [
                        {required: true, message: '开发者ID:默认为0不能为空', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '状态:0-无效 1-有效不能为空', trigger: 'blur'}
                    ],
                    isPersist: [
                        {required: true, message: '保留数据0-否 1-是 不允许删除不能为空', trigger: 'blur'}
                    ],
                },
                formItem: {
                    appId: '',
                    apiKey: '',
                    secretKey: '',
                    appName: '',
                    appNameEn: '',
                    appIcon: '',
                    appType: '',
                    appDesc: '',
                    appOs: '',
                    website: '',
                    developerId: '',
                    status: '',
                    isPersist: '',
                },
                columns: [
                    {
                        title: 'API访问key',
                        key: 'apiKey',
                        width: 100
                    },
                    {
                        title: 'API访问密钥',
                        key: 'secretKey',
                        width: 100
                    },
                    {
                        title: 'app名称',
                        key: 'appName',
                        width: 100
                    },
                    {
                        title: 'app英文名称',
                        key: 'appNameEn',
                        width: 100
                    },
                    {
                        title: '应用图标',
                        key: 'appIcon',
                        width: 100
                    },
                    {
                        title: 'app类型:server-服务应用 app-手机应用 pc-PC网页应用 wap-手机网页应用',
                        key: 'appType',
                        width: 100
                    },
                    {
                        title: 'app描述',
                        key: 'appDesc',
                        width: 100
                    },
                    {
                        title: '移动应用操作系统:ios-苹果 android-安卓',
                        key: 'appOs',
                        width: 100
                    },
                    {
                        title: '官网地址',
                        key: 'website',
                        width: 100
                    },
                    {
                        title: '开发者ID:默认为0',
                        key: 'developerId',
                        width: 100
                    },
                    {
                        title: '状态:0-无效 1-有效',
                        key: 'status',
                        width: 100
                    },
                    {
                        title: '保留数据0-否 1-是 不允许删除',
                        key: 'isPersist',
                        width: 100
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        fixed: 'right',
                        width: 120
                    }
                ],
                data: []
            }
        },
        methods: {
            handleModal (data) {
                if (data) {
                    this.modalTitle = '编辑系统应用-基础信息'
                    this.formItem = Object.assign({}, this.formItem, data)
                } else {
                    this.modalTitle = '添加系统应用-基础信息'
                }
                this.modalVisible = true
            },
            handleReset () {
                const newData = {
                    appId: '',
                    apiKey: '',
                    secretKey: '',
                    appName: '',
                    appNameEn: '',
                    appIcon: '',
                    appType: '',
                    appDesc: '',
                    appOs: '',
                    website: '',
                    developerId: '',
                    status: '',
                    isPersist: '',
                }
                this.formItem = newData
                //重置验证
                let form  = this.refs['form']
                form.resetFields()
                        this.modalVisible = false
                this.saving = false
            },
            handleSubmit () {
                let form  = this.refs['form']
                form.validate((valid) => {
                    if (valid) {
                        this.saving = true
                        if (this.formItem.appId) {
                        BaseApp.update(this.formItem).then(res => {
                            if (res.code === 0) {
                                this.$Message.success('保存成功')
                                this.handleReset()
                             }
                            this.handleSearch()
                        }).finally(() => {this.saving = false})
                    } else {
                       BaseApp.add(this.formItem).then(res => {
                            this.handleReset()
                            this.handleSearch()
                            if (res.code === 0) {
                                this.$Message.success('保存成功')
                            }
                          }).finally(() => {this.saving = false})
                        }
                   }
                })
            },
            handleSearch (page) {
                if (page) {
                    this.pageInfo.page = page
                }
                this.loading = true
                BaseApp.list({page: this.pageInfo.page, limit: this.pageInfo.limit}).then(res => {
                    this.data = res.data.records
                    this.pageInfo.total = parseInt(res.data.total)
                }).finally(() => {this.loading = false})
            },
            handlePage (current) {
                this.pageInfo.page = current
                this.handleSearch()
            },
            handlePageSize (size) {
                this.pageInfo.limit = size
                this.handleSearch()
            },
            handleRemove (data) {
               let modal = this.$Modal
               modal.confirm({
                    title: '确定删除吗？',
                    onOk: () => {
                        BaseApp.remove(data.appId).then(res => {
                            if (res.code === 0) {
                                this.pageInfo.page = 1;
                                this.$Message.success('删除成功');
                            }
                            this.handleSearch();
                        })
                    }
                })
            },
            handleClick (name, row) {
                switch (name) {
                    case 'remove':
                        this.handleRemove(row)
                        break
                }
            }
        },
        mounted: function () {
            this.handleSearch()
        }
    }
</script>
